package com.jingdianjichi.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.enums.IsDeletdFlageEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SubjectLabelDomainServiceImpl 类
 *
 * @author 熊苏鹏1
 * @date 2025/2/14 20:53
 * @description 标签Domain实现类
 */
@Slf4j
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    SubjectCategoryService categoryService;
    @Resource
    SubjectLabelService subjectLabelService;
    @Resource
    SubjectMappingService subjectMappingService;
    @Override
    public boolean add(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled())
            log.info("SubjectLabelDomainServiceImpl add bo:{}", JSON.toJSONString(subjectLabelBO));
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
        subjectLabel.setIsDeleted(IsDeletdFlageEnum.NOT_DELETED.getCode());
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    @Override
    public boolean update(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled())
            log.info("SubjectLabelDomainServiceImpl update bo:{}", JSON.toJSONString(subjectLabelBO));
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public boolean delete(SubjectLabelBO subjectLabelBO){
        if(log.isInfoEnabled())
            log.info("SubjectLabelDomainServiceImpl delete bo:{}", JSON.toJSONString(subjectLabelBO));
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletdFlageEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        SubjectCategory subjectCategory = categoryService.queryById(subjectLabelBO.getCategoryId());
        if(subjectCategory.getCategoryType() == 1){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> subjectLabel1 = subjectLabelService.queryCondition(subjectLabel);
            return SubjectLabelConverter.INSTANCE.convertLabelToBoList(subjectLabel1);
        }
        if(log.isInfoEnabled())
            log.info("SubjectLabelDomainServiceImpl queryLabelByCategoryId bo:{}", JSON.toJSONString(subjectLabelBO));
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(subjectLabelBO.getCategoryId());
        subjectMapping.setIsDeleted(IsDeletdFlageEnum.NOT_DELETED.getCode());
        List<SubjectMapping> subjectMappings = subjectMappingService.queryLabelId(subjectMapping);
        if(CollectionUtils.isEmpty(subjectMappings)){
            return Collections.emptyList();
        }
        List<Long> labelIds = subjectMappings.stream()
                .map(SubjectMapping::getLabelId)
                .collect(Collectors.toList());
        List<SubjectLabel> subjectLabels = subjectLabelService.queryBatchById(labelIds);
        List<SubjectLabelBO> subjectLabelBOS = SubjectLabelConverter.INSTANCE.convertLabelToBoList(subjectLabels);
        subjectLabelBOS.forEach(subjectLabelBO1 -> subjectLabelBO1.setCategoryId(subjectLabelBO.getCategoryId()));
        return subjectLabelBOS;
    }
}
